package com.kgc.crowd.distributioncrowddatabaseprovider.service.impl;

import com.kgc.crow.distributioncrowdcommon.pojo.MemberLaunchInfoPO;
import com.kgc.crow.distributioncrowdcommon.pojo.MemberLaunchInfoPOExample;
import com.kgc.crow.distributioncrowdcommon.pojo.MemberPo;
import com.kgc.crow.distributioncrowdcommon.pojo.MemberPoExample;
import com.kgc.crow.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.MemberLaunchInfoPOMapper;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.MemberPoMapper;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    MemberPoMapper memberPoMapper;

    @Resource
    MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Override
    public Integer getLoignAcctCount(String loginAcct) {
        // 1.创建MemberPOExample
        MemberPoExample example = new MemberPoExample();
        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);
        // 3.执行查询
        int i = memberPoMapper.countByExample(example);
        return i;
    }

    @Override
    public void saveMemberPO(MemberPo memberPO) {
        memberPoMapper.insert(memberPO);
    }

    @Override
    public MemberPo getMemberByLoginAcct(String loginAcct) {
        // 1.创建MemberPOExample对象
        MemberPoExample example = new MemberPoExample();

        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);

        // 3.执行QBC查询
        List<MemberPo> memberList = memberPoMapper.selectByExample(example);

        // 4.判断查询结果集合的有效性
        if(CrowdUtils.collectionEffectiveCheck(memberList)) {

            // 5.如果查询结果集合有效，则返回第一个元素
            return memberList.get(0);
        }

        // 6.如果集合无效则返回null
        return null;
    }

    @Override
    public MemberLaunchInfoPO getMemberLaunchInfoPOByMember(Integer memberId) {
        MemberLaunchInfoPOExample memberLaunchInfoPOExample = new MemberLaunchInfoPOExample();
        MemberLaunchInfoPOExample.Criteria criteria = memberLaunchInfoPOExample.createCriteria();
        criteria.andMemberidEqualTo(memberId);
        List<MemberLaunchInfoPO> memberLaunchInfoPOS = memberLaunchInfoPOMapper.selectByExample(memberLaunchInfoPOExample);
        if(CrowdUtils.collectionEffectiveCheck(memberLaunchInfoPOS)){
            MemberLaunchInfoPO memberLaunchInfoPO = memberLaunchInfoPOS.get(0);
            return memberLaunchInfoPO;
        }
        return null;
    }
}
